package middleware

import (
	"github.com/gin-gonic/gin"
	"newbeemallStudyProject/model/common/response"
	"newbeemallStudyProject/service"
	"time"
)

var usertoken = service.ServiceGroupApp.MallServiceGroup.MallUserTokenService

func UserJWTAuth() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.GetHeader("token")
		if len(token) == 0 {
			response.UnLogin(c, nil)
			c.Abort()
			return
		}
		err, userToken := usertoken.ValidToken(token)
		if err != nil {
			response.UnLogin(c, nil)
			c.Abort()
			return
		}
		//判断过期时间是否大于当前时间 若大于 判定token失效
		if userToken.ExpireTime.Before(time.Now()) {
			usertoken.DeleteToken(token)
			response.UnLogin(c, "授权已过期")
			c.Abort()
			return
		}
		c.Next()
	}
}
